
package com.hitechbangla.fctu.form;

import com.hitechbangla.fctu.base.PressedButtonEnum;
import fctu.simulator.HTB_MAIN;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
import com.hitechbangla.fctu.base.aircraft.Aircraft;
import com.hitechbangla.fctu.base.radar.Radar;
import com.hitechbangla.fctu.utility.GeneralUtil;

/**
 *
 * @author Asus
 */
public class Gamecontrol extends javax.swing.JInternalFrame 
{
    HTB_MAIN htb_main;
    Timer timer;
    RadarTimerTask radarTimerTask;
    private boolean  aircraftContinue;
    
    
    public Gamecontrol(HTB_MAIN htb_main, boolean b)
    {
        this.htb_main = htb_main;
        
	aircraftContinue = b;
    }
    
    public Gamecontrol(HTB_MAIN htb_main) 
    { 
        super("", true, true, true, true);
        this.htb_main = htb_main;
        initComponents();
        jButtonRecord.setVisible(false);
	aircraftContinue = true;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jButtonStart = new javax.swing.JButton();
        jButtonRecord = new javax.swing.JButton();
        jButtonStop = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Game Control");
        setAlwaysOnTop(true);

        jButtonStart.setText("Start");
        jButtonStart.setPreferredSize(new java.awt.Dimension(75, 23));
        jButtonStart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonStartActionPerformed(evt);
            }
        });

        jButtonRecord.setText("Record");
        jButtonRecord.setPreferredSize(new java.awt.Dimension(75, 23));
        jButtonRecord.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRecordActionPerformed(evt);
            }
        });

        jButtonStop.setText("Stop");
        jButtonStop.setPreferredSize(new java.awt.Dimension(75, 23));
        jButtonStop.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonStopActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonRecord, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonStop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(30, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButtonStart, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonStop, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButtonRecord, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(33, 33, 33))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(85, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(56, 56, 56))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(55, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void gameStartNow() {
        gameStartAction();
    }
    
    public void gamePauseNow() {
        gameStopAction();
    }

    private void setAlwaysOnTop(boolean b) {
       // throw new UnsupportedOperationException("Not yet implemented");
    }
    
    class RadarTimerTask extends TimerTask
    {
	List<Aircraft> listAircraft = htb_main.gameScene.airCraftList;        
            public void run()
            {
		if(aircraftContinue)
		{
		    for(Aircraft aircraft: listAircraft)
		    {
			aircraft.update(1000);
			////System.out.println("Call sign: "+ aircraft.getAircraftState().getCallSign() + " x:" + aircraft.getAircraftState().getLocation().getX() + ", y:" + aircraft.getAircraftState().getLocation().getY());
		    }    
		}
                
                
            }
    }
    
    private void gameStartAction()
    {
        aircraftContinue = true;
	
	for( Radar radar: htb_main.gameScene.radarList)
	{
	    radar.setRadarContinue(true);
	}
        
        for( Radar radar: htb_main.gameScene.radarList) {
           radar.updateRadar(); 
        }
        
	htb_main.flagDrawRadar = true;
	htb_main.lastPressedButton = PressedButtonEnum.NONE;
	
        
        timer = new Timer();        
	radarTimerTask = new RadarTimerTask();
	
	timer.scheduleAtFixedRate(radarTimerTask, 0, 1000);
	htb_main.gameStarted = true;
        htb_main.repaint();
       
    }
    
    private void jButtonStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonStartActionPerformed
             
        //htb_main.gameScene.radarList.get(0).updateRadar();
        gameStartAction();
	
	//GeneralUtil.StartGame(htb_main.gameScene);
        GeneralUtil.UpdateGameScene(htb_main.gameScene);
	
	/*
        timer.scheduleAtFixedRate(new TimerTask()
        {            
            List<Aircraft> listAircraft = htb_main.gameScene.airCraftList;        
            public void run()
            {
                for(Aircraft aircraft: listAircraft)
                {
                    aircraft.update(1000);
                    ////System.out.println("Call sign: "+ aircraft.getAircraftState().getCallSign() + " x:" + aircraft.getAircraftState().getLocation().getX() + ", y:" + aircraft.getAircraftState().getLocation().getY());
                }
                
                
                
            }
        },0,1000
               
        );
        htb_main.gameStarted = true;
        htb_main.repaint();
        */
    }//GEN-LAST:event_jButtonStartActionPerformed

    
    private void gameStopAction()
    {
        aircraftContinue = false;
	
	for( Radar radar: htb_main.gameScene.radarList)
	{
	    radar.setRadarContinue(false);
            radar.stopTimer();
	}
        
    }
    
    private void jButtonStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonStopActionPerformed
	
        gameStopAction();
        GeneralUtil.StopGame(this.htb_main.gameScene);
        
	
    }//GEN-LAST:event_jButtonStopActionPerformed

    private void jButtonRecordActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRecordActionPerformed
        
    }//GEN-LAST:event_jButtonRecordActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Gamecontrol.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Gamecontrol.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Gamecontrol.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Gamecontrol.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Gamecontrol(null).setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonRecord;
    private javax.swing.JButton jButtonStart;
    private javax.swing.JButton jButtonStop;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
